package com.yiren.manager.service.impl.lease;

import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.yiren.core.exception.MyException;
import com.yiren.core.service.GenericServiceImpl;
import com.yiren.core.utils.Favorites;
import com.yiren.entity.YrElderPayRecord;
import com.yiren.entity.YrElderWallet;
import com.yiren.entity.YrLease;
import com.yiren.entity.YrLeasePledge;
import com.yiren.entity.YrLeaseRentRecord;
import com.yiren.entity.YrLeaseSupply;
import com.yiren.entity.YrRetreatLease;
import com.yiren.manager.repository.cost.YrElderPayRecordRepository;
import com.yiren.manager.repository.elder.YrElderWalletRepository;
import com.yiren.manager.repository.lease.YrLeasePledgeRepository;
import com.yiren.manager.repository.lease.YrLeaseRentRecordRepository;
import com.yiren.manager.repository.lease.YrLeaserepository;
import com.yiren.manager.repository.lease.YrRetreatLeaseRepository;
import com.yiren.manager.repository.pension.YrLeaseSupplyRepository;
import com.yiren.manager.service.iface.lease.YrLeaseService;

@Service
public class YrLeaseServiceImpl extends GenericServiceImpl<YrLease, String> implements YrLeaseService{

	@Autowired
	private YrRetreatLeaseRepository retreatLeaseRepository;
	
	@Autowired
	private YrElderWalletRepository elderWalletRepository;
	
	@Autowired
	private YrLeaseSupplyRepository leaseSupplyRepository;
	
	@Autowired
	private YrLeasePledgeRepository leasePledgeRepository;
	
	@Autowired
	private YrElderPayRecordRepository elderPayRecordRepository;
	
	@Autowired
	private YrLeaseRentRecordRepository leaseRentRecordRepository;
	
	@Autowired
	public YrLeaseServiceImpl(YrLeaserepository repository) {
		super(repository);
	}

	@Transactional
	public void executeSurrender(Favorites entitys) throws MyException {
		YrLease lease = entitys.getFavorite(YrLease.class);
		YrRetreatLease retreatLease = entitys.getFavorite(YrRetreatLease.class);
		YrLeasePledge leasePledge = entitys.getFavorite(YrLeasePledge.class);
		YrElderWallet elderWallet = entitys.getFavorite(YrElderWallet.class);
		YrElderPayRecord elderPayRecord = entitys.getFavorite(YrElderPayRecord.class);
		YrLeaseSupply leaseSupply = entitys.getFavorite(YrLeaseSupply.class);
		YrLeaseRentRecord leaseRentRecord = entitys.getFavorite(YrLeaseRentRecord.class);
		
		this.repository.save(lease);
		retreatLeaseRepository.save(retreatLease);
		if(null!=leasePledge){
			leasePledgeRepository.save(leasePledge);
		}
		if(null!=elderWallet){
			elderWalletRepository.save(elderWallet);
		}
		if(null!=elderPayRecord){
			elderPayRecordRepository.save(elderPayRecord);
		}
		if(null!=leaseSupply){
			leaseSupplyRepository.save(leaseSupply);
		}
		if(null!=leaseRentRecord){
			leaseRentRecordRepository.save(leaseRentRecord);
		}
		

	}

	@Transactional
	public void addByFavorites(Favorites entitys) throws MyException {
		YrLease lease = entitys.getFavorite(YrLease.class);
		YrElderWallet elderWallet = entitys.getFavorite(YrElderWallet.class);
		YrElderPayRecord elderPayRecord = entitys.getFavorite(YrElderPayRecord.class);
		YrLeaseSupply leaseSupply = entitys.getFavorite(YrLeaseSupply.class);
		YrLeasePledge leasePledge = entitys.getFavorite(YrLeasePledge.class);
		YrLeaseRentRecord leaseRentRecord = entitys.getFavorite(YrLeaseRentRecord.class);
		
		this.repository.save(lease);
		if(null != elderWallet){
			elderWalletRepository.save(elderWallet);
		}
		if(null!=elderPayRecord){
			elderPayRecordRepository.save(elderPayRecord);
		}
		if(null!=leaseSupply){
			leaseSupplyRepository.save(leaseSupply);
		}
		if(null!=leasePledge){
			leasePledgeRepository.save(leasePledge);
		}
		if(null!=leaseRentRecord){
			leaseRentRecordRepository.save(leaseRentRecord);
		}
	}

	/**
	 * 续租
	 */
	@Transactional
	public void addLease(Favorites entitys) throws MyException {
		YrLease lease = entitys.getFavorite(YrLease.class);
		YrElderWallet elderWallet = entitys.getFavorite(YrElderWallet.class);
		YrElderPayRecord elderPayRecord = entitys.getFavorite(YrElderPayRecord.class);
		YrLeaseRentRecord leaseRentRecord = entitys.getFavorite(YrLeaseRentRecord.class);
		
		this.repository.save(lease);
		if(null!=elderWallet){
			elderWalletRepository.save(elderWallet);
		}
		if(null!=elderPayRecord){
			elderPayRecordRepository.save(elderPayRecord);
		}
		if(null!=leaseRentRecord){
			leaseRentRecordRepository.save(leaseRentRecord);
		}
		
	}
	
	
}
